import $ from 'zepto';

export default {
    get(url, data) {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: url,
                type: 'GET',
                dataType: 'json',
                data: data,
                success: function(res){
                    var response = res;
                    if (response.ret == 0) {
                        resolve(response.data);
                    } else {
                        reject(response);
                    }
                },
                error: function(res){
                    reject(res);
                }
            });
        })
    },
    post(url, data, headerType) {
        return new Promise((resolve, reject) => {
            var headers = {};
            var formData = new FormData();

            if(headerType == 'payLoad'){
                data = JSON.stringify(data);
                headers['Content-Type'] = 'application/json;charset=utf-8';
            }

            if(headerType == 'formData'){
                for(let key in data){
                    formData.append(key,data[key]);
                }
            }

            $.ajax({
                url: url,
                type: 'POST',
                dataType: 'json',
                data: (headerType == 'formData'?formData:data),
                headers: headers,
                processData: ((headerType == 'formData'||headerType == 'uploader')?false:true),
                contentType: (headerType == 'uploader'?false:''),
                success: function(res){
                    var response = res;
                    if (response.ret == 0) {
                        resolve(response.data);
                    } else {
                        reject(response);
                    }
                },
                error: function(res){
                    reject(res);
                }
            });
        })
    },
    uploader(url, data) {
        return new Promise((resolve, reject) => {
            ctx.$http.post(url, data).then((res) => {
                var response = res.body;
                if(response.ret==0){
                    resolve(response.data);
                }else{
                    reject(response.msg);                    
                }
            }, (res) => {
                reject(res);
                // console.log(res);
            })
        });
    },
    request(type = 'POST', url, data) {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: url,
                type: type,
                dataType: 'json',
                data: data,
                success: function(res){
                    var response = res;
                    if (response.ret == 0) {
                        resolve(response.data);
                    } else {
                        reject(response);
                    }
                },
                error: function(res){
                    reject(res);
                }
            });
        })
    },
    json2Form(json) {
        var str = []
        for (var p in json) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]))
        }
        return str.join("&")
    },
    serializeObject(form) {
        var obj = {};
        var data = form.serializeArray();
        $.each(data, function() {
            if (obj[this.name] !== undefined) {
                if (!obj[this.name].push) {
                    obj[this.name] = [obj[this.name]];
                }
                obj[this.name].push(this.value || '');
            } else {
                obj[this.name] = this.value || '';
            }

            // related 关联元素隐藏就不提交
            if ($('[name="' + this.name + '"]').parents('.form-group').hasClass('hide')) {
                delete obj[this.name];
            }
        });

        $('textarea').each(function() {
            var name = $(this).attr('name');
            if (obj[name]) {
                obj[name] = obj[name].replace(/\r\n/g, '￥￥');
            }
        });
        // console.log(obj)
        return obj;
    }
}
